package com.example.myapplication.SQLite;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.R;

public class SQLiteActivity extends AppCompatActivity {

    EditText userName;
    EditText userPwd;
    DatabaseHelper helper;
    SQLiteDatabase db;
    String sql;

    String uname;
    String upwd;

    Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);

        helper = new DatabaseHelper(this,"demo.db",null,2);
        db = helper.getWritableDatabase();
        userName = findViewById(R.id.userName);
        userPwd = findViewById(R.id.userPwd);

    }

    @SuppressLint("Range")
    public void btnClick(View view) {
        uname = userName.getText().toString();
        upwd = userPwd.getText().toString();
        switch (view.getId()){
            case R.id.btn1:
                sql = "insert into user(username,userpwd) values('"+uname+"','"+upwd+"')";
                db.execSQL(sql);
                userName.setText("");
                userPwd.setText("");
                Toast.makeText(this,"创建用户成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn2:
                ///方法一
                /*sql = "update user set userpwd='"+upwd+"' where username='"+uname+"'";
                db.execSQL(sql);
                userName.setText("");
                userPwd.setText("");
                Toast.makeText(this,"修改密码成功",Toast.LENGTH_SHORT).show();*/
                ///方法二
                ContentValues values = new ContentValues();
                values.put("userpwd",upwd);
                db.update("user",values,"username=?",new  String[]{uname});
                break;
            case R.id.btn3:
                ///查询语句
                sql = "select * from user";
                ///条件
                String[] args = null;
                if(uname != null&&!uname.equals("")){
                    if(!uname.equals("all")){
                        ///添加username
                        sql = sql + " where username=?";
                        args = new String[]{uname};
                        if(upwd != null&&!upwd.equals("")){
                            ///添加userpwd
                            sql = sql + " and userpwd=?";
                            args = new String[]{uname,upwd};
                        }
                    }else{
                        ///如果字符是all,则查询全部
                        args = null;
                    }
                    cursor = db.rawQuery(sql,args);
                    cursor.moveToFirst();
                    String name;
                    String pwd;
                    while (!cursor.isAfterLast()){
                        name = cursor.getString(cursor.getColumnIndex("username"));
                        pwd = cursor.getString(cursor.getColumnIndex("userpwd"));
                        Log.e("DATABASE", "用户名: "+name+","+"密码: "+pwd );
                        cursor.moveToNext();
                    }
                    Toast.makeText(this,"请输入用户名",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this,"查询用户成功",Toast.LENGTH_SHORT).show();

                }
                break;
            case R.id.btn4:
                /*sql = "delete from user where username='"+uname+"'";
                db.execSQL(sql);
                userName.setText("");
                userPwd.setText("");
                Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show();*/

                db.delete("user","username=?",new  String[]{uname});

                break;
            default:
                break;
        }
    }
}